package com.derbysoft.dswitch.adapter.ihg.dswitch2.service.impl;

import com.derbysoft.dswitch.adapter.ihg.common.log.LogBody;
import com.derbysoft.dswitch.adapter.ihg.common.log.PerformanceLogger;
import com.derbysoft.dswitch.adapter.ihg.common.utils.EnvironmentUtils;
import com.derbysoft.dswitch.adapter.ihg.dswitch2.HotelProviderService;
import com.derbysoft.dswitch.adapter.ihg.dswitch3.translate.Translator;
import com.derbysoft.dswitch.adapter.ihg.dto.OTAHotelAvailRQ;
import com.derbysoft.dswitch.adapter.ihg.dto.OTAHotelAvailRS;
import com.derbysoft.dswitch.adapter.ihg.webservice.handler.WebServiceHandler;
import com.derbysoft.dswitch.adapter.ihg.webservice.impl.AccountProvider;
import com.derbysoft.remote.dswitch.core.GenericRQ;
import com.derbysoft.remote.dswitch.core.GenericRS;
import com.derbysoft.remote.dswitch.core.ResponseHeader;
import com.derbysoft.remote.dswitch.hotel.dto.HotelAvailRQ;
import com.derbysoft.remote.dswitch.hotel.dto.HotelAvailRS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("ds2hotelAvailabilityWebService")
public class HotelAvailWebServiceImpl extends BaseHotelWebService
        implements HotelProviderService<GenericRQ<HotelAvailRQ>, GenericRS<HotelAvailRS>> {

    @Autowired
    @Qualifier("availWebServiceHandler")
    private WebServiceHandler<OTAHotelAvailRQ, OTAHotelAvailRS> hotelAvailWebService;

    @Autowired
    @Qualifier("ds2HotelAvailRQTranslator")
    private Translator<GenericRQ<HotelAvailRQ>, OTAHotelAvailRQ, Void> hotelAvailRQTranslator;

    @Autowired
    @Qualifier("ds2HotelAvailRSTranslator")
    protected Translator<OTAHotelAvailRS, GenericRS<HotelAvailRS>, GenericRQ<HotelAvailRQ>> hotelAvailRSTranslator;

    @Override
    public GenericRS<HotelAvailRS> invoke(GenericRQ<HotelAvailRQ> request) {
        try {
            OTAHotelAvailRQ otaHotelAvailRQ = hotelAvailRQTranslator.translate(request, null);
            String derbyChannelCode = request.getHeader().getSource().getUniqueID().getId();
            OTAHotelAvailRS otaHotelAvailRS = hotelAvailWebService.invoke(otaHotelAvailRQ, false,
                    AccountProvider.of(EnvironmentUtils.getGlobalIhgAvailabilityUrl(),
                            loadAuthorization(derbyChannelCode)));
            return hotelAvailRSTranslator.translate(otaHotelAvailRS, request);
        } catch (Exception ex) {
            GenericRS<HotelAvailRS> failedResponse = failedResponse(request, ex);
            PerformanceLogger.append(LogBody.ERROR_CODE, failedResponse.getErrors().getErrorList().get(0).getCode());
            return failedResponse;
        }
    }

    private GenericRS<HotelAvailRS> failedResponse(GenericRQ<HotelAvailRQ> request, Exception ex) {
        GenericRS<HotelAvailRS> response = new GenericRS<HotelAvailRS>();
        ResponseHeader header = new ResponseHeader();
        header.setTaskId(request.getHeader().getTaskId());
        response.setHeader(header);
        response.setErrors(createErrorDTO(ex));
        return response;
    }
}
